package com.star.truffle.core.web.config;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import com.star.truffle.core.jackson.JsonConfig;
import com.star.truffle.core.jackson.StarJson;

@Configuration
@ConditionalOnWebApplication
@ConditionalOnBean(StarJson.class)
@AutoConfigureAfter({ JsonConfig.class, SpringContextConfig.class, MvcInterceptorsConfig.class })
@EnableConfigurationProperties(StarSpringMvcProperties.class)
public class SpringMvcConfig {

//  @Bean
//  public MultipartConfigElement multipartConfigElement(StarSpringMvcProperties properties) {
//    MultipartConfigFactory factory = new MultipartConfigFactory();
//    // 单个文件最大
//    factory.setMaxFileSize(StringUtils.isBlank(properties.getMaxFileSize()) ? "8MB": properties.getMaxFileSize()); // KB,MB
//    /// 设置总上传数据总大小
//    factory.setMaxRequestSize(StringUtils.isBlank(properties.getMaxRequestSize()) ? "80MB": properties.getMaxRequestSize());
//    return factory.createMultipartConfig();
//  }
  //使用网关后，跨域移到网关，这里注释。
//  private CorsConfiguration buildConfig() {
//    CorsConfiguration corsConfiguration = new CorsConfiguration();
//    corsConfiguration.addAllowedOrigin("*");
//    corsConfiguration.addAllowedHeader("*");
//    corsConfiguration.addAllowedMethod("*");
//    return corsConfiguration;
//  }
//
//  @Bean
//  @ConditionalOnClass(CorsFilter.class)
//  @ConditionalOnProperty(value = "starWeb.cors.enabled", matchIfMissing = true)
//  public Filter corsFilter() {
//    
//    return new Filter() {
//
//      @Override
//      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
//          throws IOException, ServletException {
//        HttpServletRequest req = (HttpServletRequest) request;
//        HttpServletResponse res = (HttpServletResponse) response;
//        String origin = req.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN);
//        if(StringUtils.isBlank(origin)) {
//          origin = "*";
//        }
//        res.setHeader("Access-Control-Allow-Origin", origin);  
//        res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
//        res.setHeader("Access-Control-Max-Age", "3600");  
//        res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
//        System.out.println("*********************************过滤器被使用**************************");  
//        chain.doFilter(request, res);  
//      }
//      
//    };
////    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
////    source.registerCorsConfiguration("/**", buildConfig());
////    return new CorsFilter(source);
//  }

  // @Bean
  // @ConditionalOnClass(OncePerRequestFilter.class)
  // @ConditionalOnProperty(value = "hnWeb.log.enabled", matchIfMissing = true)
  // public Filter accessLogFilter(HnJson hnJson, HnSpringMvcProperties
  // hnSpringMvcProperties) {
  // return AccessLogFilter.of(hnJson, hnSpringMvcProperties);
  // }
}
